import path from 'path';
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import Icons from 'unplugin-icons/vite';
import IconsResolver from 'unplugin-icons/resolver';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';
import svgLoader from 'vite-svg-loader';

const pathSrc = path.resolve(__dirname, 'src');

// https://vitejs.dev/config/
export default defineConfig({
  server: {
    port: 21527,
    host: true,
    proxy: {
      '^/node/': {
        target: 'http://192.168.0.101:3014',
        // target: 'http://123.249.97.59:21526',
        // target: 'http://192.168.1.67:3004/',
        changeOrigin: true,
        rewrite: path => path.replace('/node/', '/'),
      },
      '^/area/': {
        target: 'http://192.168.0.101:21810',
        // target: 'http://123.249.97.59:21530',
        // target: 'http://192.168.1.67:21810/',
        changeOrigin: true,
        rewrite: path => path.replace('/area/', '/'),
      },
      '^/frontend/': {
        target: 'http://192.168.0.101:21811',
        // target: 'http://123.249.97.59:21532',
        // target: 'http://192.168.1.67:21811/',
        changeOrigin: true,
        rewrite: path => path.replace('/frontend/', '/'),
      },
      '^/mapboxsrc/mapbox/': {
        target: 'https://fleet.niowx.com/',
        changeOrigin: true,
        rewrite: path => path.replace('/mapboxsrc/', '/'),
      },
      '^/mapboxsrc/darkmap-retina/': {
        target: 'https://nine-confidential.obs.cn-north-4.myhuaweicloud.com/',
        changeOrigin: true,
        rewrite: path => path.replace('/mapboxsrc/', '/'),
      },
      '^/mapboxsrc/i4/': {
        target: 'https://i4.niowx.com:21664/',
        changeOrigin: true,
        rewrite: path => path.replace('/mapboxsrc/', '/'),
      },
      '^/mapboxsrc/': {
        target: 'http://192.168.0.101:21650',
        // target: 'http://123.249.97.59:21534',
        // target: 'http://192.168.1.67:21650',
        changeOrigin: true,
      },
      '^/weather/': {
        target: 'http://192.168.0.101:21650/mapboxsrc/',
        changeOrigin: true,
        rewrite: path => path.replace('/weather/', '/'),
      },
      '^/draw/': {
        target: 'http://192.168.0.101:21690',
        // target: 'http://123.249.97.59:21528',
        // target: 'http://192.168.1.67:21690/',
        changeOrigin: true,
        rewrite: path => path.replace('/draw/', '/'),
      },
    },
  },
  resolve: {
    alias: {
      '@': pathSrc,
    },
  },
  plugins: [
    vue(),
    svgLoader(),
    AutoImport({
      imports: ['vue'],
      resolvers: [
        ElementPlusResolver(),
        // Auto import icon components
        // 自动导入图标组件
        IconsResolver({
          prefix: 'Icon',
        }),
      ],
      dts: path.resolve(pathSrc, 'auto-imports.d.ts'),
    }),
    Components({
      resolvers: [
        // Auto register icon components
        // 自动注册图标组件
        IconsResolver({
          enabledCollections: ['ep'],
        }),
        // Auto register Element Plus components
        // 自动导入 Element Plus 组件
        ElementPlusResolver(),
      ],
      dts: path.resolve(pathSrc, 'components.d.ts'),
    }),
    Icons({
      autoInstall: true,
    }),
  ],
  css: {
    preprocessorOptions: {
      less: {
        charset: false,
      },
    },
  },
});
